package com.lagou.edu.factory;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.annotation.Transactional;
import com.lagou.edu.utils.TransactionManager;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;


@Service("proxyFactory")
public class ProxyFactory {

    @Autowired
    private TransactionManager transactionManager;


    //Jdk动态代理
    public Object getJdkProxy(Object obj) {

        // 获取代理对象
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Object result = null;
                        //事务管理器标志不为空，使用事务相关方法
                        if(!BeanFactory.getTransactionManager().equals("")){

                            Transactional annotationMethod = method.getAnnotation(Transactional.class);
                            if(null==annotationMethod){
                                Transactional annotationClass = obj.getClass().getAnnotation(Transactional.class);
                                if(null==annotationClass){
                                    result = method.invoke(obj, args);
                                }else {
                                    try {
                                        // 开启事务(关闭事务的自动提交)
                                        transactionManager.beginTransaction();
                                        result = method.invoke(obj, args);
                                        // 提交事务
                                        transactionManager.commit();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        // 回滚事务
                                        transactionManager.rollback();
                                        // 抛出异常便于上层servlet捕获
                                        throw e;
                                    }
                                }
                            }else {
                                try {
                                    // 开启事务(关闭事务的自动提交)
                                    transactionManager.beginTransaction();
                                    result = method.invoke(obj, args);
                                    // 提交事务
                                    transactionManager.commit();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    // 回滚事务
                                    transactionManager.rollback();
                                    // 抛出异常便于上层servlet捕获
                                    throw e;
                                }
                            }
                        }else {
                            result = method.invoke(obj, args);
                        }
                        return result;
                    }
                });

    }
}
